package com.love.web.session.filter;

import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionContext;

public class StandardSession implements Session, Serializable {
	private static final long serialVersionUID = 1L;
	
    protected static final String EMPTY_ARRAY[] = new String[0];
	
	public StandardSession(Manager manager) {
		this.manager = manager;
		init();
	}
	
	private void init(){
		this.id = manager.getSessionIdGenerator().generateSessionId();
		this.creationTime = System.currentTimeMillis();
		this.isNew = true;
		this.isDirty = false;
		this.expiring = false;
	}
	
    protected transient Manager manager = null;
	
    protected ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();

    protected long creationTime = 0L;
    
    protected volatile long thisAccessedTime = creationTime;
    
    @Deprecated
    protected static volatile
            javax.servlet.http.HttpSessionContext sessionContext = null;
    
    protected transient volatile boolean expiring = false;
    
    protected String id = null;
    
    protected volatile long lastAccessedTime = creationTime;
    
    protected int maxInactiveInterval = -1;


    protected boolean isNew = false;

    protected transient boolean isDirty = false;

	public Object getAttribute(String name) {
		if (name == null){
			return null;
		}
		return attributes.get(name);
	}
	

	public Enumeration getAttributeNames() {
		Set<String> names = new HashSet<String>();
        names.addAll(attributes.keySet());
        return Collections.enumeration(names);
	}

	public long getCreationTime() {
		return this.creationTime;
	}
	

	public String getId() {
		return this.id;
	}
	
	public long getLastAccessedTime() {
		return this.lastAccessedTime;
	}

	public int getMaxInactiveInterval() {
		return this.maxInactiveInterval;
	}

	

	public HttpSessionContext getSessionContext() {
		return null;
	}

    protected String[] keys() {
        return attributes.keySet().toArray(EMPTY_ARRAY);
    }


	public String[] getValueNames() {
		return (keys());
	}

    public void invalidate() {
    	expiring = true;
		isDirty = true;
    }

	public boolean isNew() {
		return this.isNew;
	}

	public void putValue(String name, Object value) {
		setAttribute(name, value);
	}

	public void removeAttribute(String name) {
        if (name == null) {
        	return;
        }
        attributes.remove(name);
        isDirty = true;
	}

	public void removeValue(String name) {
		removeAttribute(name);
	}

	public void setAttribute(String name, Object value) {
		attributes.put(name, value);
		isDirty = true;
	}

	public void setMaxInactiveInterval(int interval) {
		this.maxInactiveInterval = interval;
	}


	public ServletContext getServletContext() {
		if(this.manager == null){
			return null;
		}
		return this.manager.getContext();
	}


	public Object getValue(String name) {
		return getAttribute(name);
	}
	
	//************************************new method***************************


	public void access() {
		this.thisAccessedTime = System.currentTimeMillis();
	}

	public void endAccess() {
		isNew = false;
		this.lastAccessedTime = this.thisAccessedTime;
	}

	public boolean isDirty() {
		return this.isDirty;
	}
	
	public boolean isExpiring() {
		return this.expiring;
	}

}
